package com.daka.controller;

import com.daka.entity.HatArea;
import com.daka.entity.HatCity;
import com.daka.entity.HatProvince;
import com.daka.enumerate.StatusCode;
import com.daka.service.AreaService;
import com.daka.service.qnzyz.BHConstant;
import com.daka.service.qnzyz.ConfigUtil;
import com.daka.vo.response.DataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/9/18 15:18
 */

@Controller
@RequestMapping("/area")
@Slf4j
public class AreaController {

    @Autowired
    private AreaService areaService;
    @Autowired
    BHConstant bhConstant;


    @ResponseBody
    @RequestMapping(value = "/queryProvince")
    public DataResponseVo<Object> queryProvince(){
        try{
            List<HatProvince> list = areaService.selectAll();
            return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(), list);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SERVER_EXCEPTION.getCode(), StatusCode.SERVER_EXCEPTION.getMessage());
        }

    }

    @ResponseBody
    @RequestMapping(value = "/queryCity" ,method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> queryCity(@RequestBody HatCity po){
        try {
            if(po.getFather()==null){
                return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(),StatusCode.COMMON_PARAM_ERROR.getMessage());
            }
            List<HatCity> list = areaService.selectByProvince(po.getFather());
            return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(), list);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SERVER_EXCEPTION.getCode(), StatusCode.SERVER_EXCEPTION.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/queryDistrict" ,method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> queryDistrict(@RequestBody HatArea po){
        try {
            if(po.getFather()==null){
                return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(),StatusCode.COMMON_PARAM_ERROR.getMessage());
            }
            List<HatArea> list = areaService.selectByCity(po.getFather());
            return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(), list);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SERVER_EXCEPTION.getCode(), StatusCode.SERVER_EXCEPTION.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/test")
    public DataResponseVo<Object> test(){
        ConfigUtil configUtil = new ConfigUtil();
        configUtil.buildConfig();
        String str = "clientId:"+ConfigUtil.clientId + "   key:"+ConfigUtil.pubKey + "bh_url:"+ bhConstant.checkNetWork();
        return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(),str);
    }

}
